package cn.iocoder.yudao.module.pay.controller.admin.point;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.pay.controller.admin.point.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.point.PayPointRechargePackageDO;
import cn.iocoder.yudao.module.pay.service.point.PayPointRechargePackageService;

@Tag(name = "管理后台 - 充值套餐")
@RestController
@RequestMapping("/pay/point-recharge-package")
@Validated
public class PayPointRechargePackageController {

    @Resource
    private PayPointRechargePackageService pointRechargePackageService;

    @PostMapping("/create")
    @Operation(summary = "创建充值套餐")
    @PreAuthorize("@ss.hasPermission('pay:point-recharge-package:create')")
    public CommonResult<Long> createPointRechargePackage(@Valid @RequestBody PayPointRechargePackageSaveReqVO createReqVO) {
        return success(pointRechargePackageService.createPointRechargePackage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新充值套餐")
    @PreAuthorize("@ss.hasPermission('pay:point-recharge-package:update')")
    public CommonResult<Boolean> updatePointRechargePackage(@Valid @RequestBody PayPointRechargePackageSaveReqVO updateReqVO) {
        pointRechargePackageService.updatePointRechargePackage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除充值套餐")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pay:point-recharge-package:delete')")
    public CommonResult<Boolean> deletePointRechargePackage(@RequestParam("id") Long id) {
        pointRechargePackageService.deletePointRechargePackage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得充值套餐")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pay:point-recharge-package:query')")
    public CommonResult<PayPointRechargePackageRespVO> getPointRechargePackage(@RequestParam("id") Long id) {
        PayPointRechargePackageDO pointRechargePackage = pointRechargePackageService.getPointRechargePackage(id);
        return success(BeanUtils.toBean(pointRechargePackage, PayPointRechargePackageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得充值套餐分页")
    @PreAuthorize("@ss.hasPermission('pay:point-recharge-package:query')")
    public CommonResult<PageResult<PayPointRechargePackageRespVO>> getPointRechargePackagePage(@Valid PayPointRechargePackagePageReqVO pageReqVO) {
        PageResult<PayPointRechargePackageDO> pageResult = pointRechargePackageService.getPointRechargePackagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PayPointRechargePackageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出充值套餐 Excel")
    @PreAuthorize("@ss.hasPermission('pay:point-recharge-package:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPointRechargePackageExcel(@Valid PayPointRechargePackagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PayPointRechargePackageDO> list = pointRechargePackageService.getPointRechargePackagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "充值套餐.xls", "数据", PayPointRechargePackageRespVO.class,
                        BeanUtils.toBean(list, PayPointRechargePackageRespVO.class));
    }

}